package team.se.tms.controller.label;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import team.se.tms.annotation.CheckFormat;
import team.se.tms.annotation.Regex;
import team.se.tms.controller.BaseController;
import team.se.tms.dto.Page;
import team.se.tms.exception.DeleteException;
import team.se.tms.exception.InsertException;
import team.se.tms.exception.UpdateException;
import team.se.tms.po.LabelInfo;
import team.se.tms.service.LabelInfoService;
import team.se.tms.util.ResultUtil;

import java.util.List;

/**
 * 标签控制层
 *
 * @author johnny
 * @date 2018/11/21
 */
@Controller
@RequestMapping("label")
public class LabelInfoController extends BaseController{
    @Autowired
    public LabelInfoService labelInfoService;
    /**
     * 去标签列表页
     *
     * @return String 页面地址
     * @throws Exception 异常
     * @create on 2018/12/04 by johnny
     */
    @RequestMapping("list")
    public String toViewAllLabels() throws Exception{
        return "label/label_list";
    }
    /**
     * 去添加标签页
     *
     * @return String 页面地址
     * @throws Exception 异常
     * @create on 2018/11/21 by johnny
     */
    @RequestMapping("toAdd")
    public ModelAndView toAddLabelInfo() throws Exception{
        ModelAndView mv = this.getModelAndView();
        mv.addObject("dotype","add");
        mv.setViewName("label/modify_label");
        return mv;
    }
    /**
     * 去修改标签页
     *
     * @param labelInfo 待修改的标签信息
     * @return String 页面地址
     * @throws Exception 异常
     * @create on 2018/11/21 by johnny
     */
    @RequestMapping("toEdit")
    public ModelAndView toEditLabel(LabelInfo labelInfo) throws Exception{
        ModelAndView mv = this.getModelAndView();
        mv.addObject("labelInfo",labelInfo);
        mv.addObject("dotype","edit");
        mv.setViewName("label/modify_label");
        return mv;
    }

    /**
     * 按分页查询标签
     *
     * @param page 分页
     * @return String 查询到的标签的json字符串
     * @throws Exception 异常
     * @create on 2018/11/21 by johnny
     */
    @RequestMapping("getList")
    @ResponseBody
    public String getLabelList(Page page) throws Exception{
        System.out.println("page.getOrderDir()"+page.getOrderDir());
        List<LabelInfo> labelInfos=  labelInfoService.listAllLabelInfos(page);
        //将查询到的信息，转化为datatable要求的json形式
        return page.getDataTabelJson(labelInfos);
    }


    /**
     * 添加标签
     *
     * @param labelInfo 标签信息
     * @return Object 添加结果
     * @throws Exception 异常
     * @create on 2018/11/21 by johnny
     */
    @RequestMapping("add")
    @ResponseBody
    @CheckFormat({"labelId"})
    public Object addLabelInfo(LabelInfo labelInfo) throws Exception {
        boolean result = labelInfoService.saveLabelInfo(labelInfo);
        if(!result){
            throw new InsertException();
        }
        return  ResultUtil.success();
    }

    /**
     * 修改标签
     *
     * @param labelInfo 标签信息
     * @return Object 修改结果
     * @throws Exception 异常
     * @create on 2018/11/21 by johnny
     */
    @RequestMapping("edit")
    @ResponseBody
    @CheckFormat
    public Object editLabelInfo(LabelInfo labelInfo) throws Exception{
        boolean result = labelInfoService.modifyLabelInfo(labelInfo);
        if(!result){
            throw new UpdateException();
        }
        return  ResultUtil.success();
    }

    /**
     * 删除标签
     *
     * @param  labelId 标签id
     * @return Object 删除结果
     * @throws Exception 异常
     * @create on 2018/11/21 by johnny
     */
    @RequestMapping("drop")
    @ResponseBody
    @CheckFormat
    public Object dropLabelInfo(@Regex("^[A-Za-z0-9]{16}$") String labelId) throws Exception{
        boolean result = labelInfoService.removeLabelInfo(labelId);
        if(!result){
            throw new DeleteException();
        }
        return  ResultUtil.success();
    }


}
